import pygame
import random
from pygame.locals import *
#面向对象的方式
class HeroPlan(object):
    def __init__(self,screen):
        self.x=600
        self.y=600
        # 默认位置
        self.screen = screen
        #生成飞机图片对象
        self.imageName='C:/Users/25238/Pictures/Saved Pictures/强哥缩小版.png'
        self.image=pygame.image.load(self.imageName)
        self.bulletlist=[]#存放子弹
        pass
    def moveleft(self):
        if self.x > 0 :
            self.x -= 30
        pass
    def moveright(self):
        if self.x < 600 :
            self.x += 30
        pass
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        needDelitemlist=[]
        for item in self.bulletlist:
            if item.judge():
                needDelitemlist.append(item)
                pass
            pass
        for i in needDelitemlist:
            self.bulletlist.remove(i)
            pass
        for bullet in self.bulletlist:
            bullet.display()
            bullet.move()
        pass
    def sheBullt(self):
        newBullet = Bullet(self.x,self.y,self.screen)
        self.bulletlist.append(newBullet)
class Bullet(object):
    def __init__(self,x,y,screen):
        self.x = x+15
        self.y = y
        self.screen = screen
        self.image = pygame.image.load('C:/Users/25238/Pictures/Saved Pictures/拳头.png')
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        pass
    def move(self):
        self.y -= 0.1
        pass
    def judge(self):
        if self.y < 0:
            return True
        else:
            return False
        pass
class EnemyBullet(object):
    def __init__(self,x,y,screen):
        self.x = x
        self.y = y+20
        self.screen = screen
        self.image = pygame.image.load('C:/Users/25238/Pictures/Saved Pictures/飞吻.png')
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        pass
    def move(self):
        self.y += 0.1
        pass
    def judge(self):
        if self.y > 700:
            return True
        else:
            return False
        pass
class EnemyPlane():
    def __init__(self,screen):
        self.direction = 'right'
        self.screen = screen
        self.bulletlist = []
        self.x = 0
        self.y = 0
        self.imageName = 'C:/Users/25238/Pictures/Saved Pictures/美女敌人.png'
        self.image = pygame.image.load(self.imageName)
        pass
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        needDelitemlist = []
        for item in self.bulletlist:
            if item.judge():
                needDelitemlist.append(item)
                pass
            pass
        for i in needDelitemlist:
            self.bulletlist.remove(i)
            pass
        for bullet in self.bulletlist:
            bullet.display()
            bullet.move()
        pass
    def sheBullet(self):
        num = random.randint(1,1000)
        if num == 3 :
            newBullet = EnemyBullet(self.x,self.y,self.screen)
            self.bulletlist.append(newBullet)
            pass
        pass
    def move(self):
        if self.direction == 'right':
            self.x += 0.1
            pass
        elif self.direction == 'left':
            self.x -= 0.1
            pass
        if self.x > 650:
            self.direction = 'left'
            pass
        elif self.x < 0:
            self.direction = 'right'
            pass
        pass
def key_control(HeroObj):
    enventlist = pygame.event.get()
    for event in enventlist:
        if event.type == QUIT:
            print('退出')
            exit()
            pass
        elif event.type == KEYDOWN:
            if event.type == K_a or event.key == K_LEFT:
                print('left')
                HeroObj.moveleft()
                pass
            elif event.type == K_d or event.key == K_RIGHT:
                print('right')
                HeroObj.moveright()
                pass
            elif event.key == K_SPACE:
                print('K_SPACE')
                HeroObj.sheBullt()
def main():    #创建窗口
    screen = pygame.display.set_mode((700,700),depth=32)
    #设定背景图片
    background = pygame.image.load('C:/Users/25238/Pictures/Saved Pictures/蓝天.png')
    #创建一个飞机对象
    hero = HeroPlan(screen)
    enemy = EnemyPlane(screen)
    while True:
        screen.blit(background,(0,0))
        hero.display()
        key_control(hero)
        enemy.display()
        enemy.move()
        enemy.sheBullet()
        pygame.display.update()
        #更新显示的内容
    pass
if __name__ == '__main__':
    main()